const process = require('process');
const axios = require('axios');
const cheerio = require('cheerio');

const DOMAIN = 'https://y.qq.com';

const RNK_LIST_URL = 'https://y.qq.com/n/ryqq/toplist';


const getRankListData = async (rankType) => {
    const { data } = await axios.get(`${RNK_LIST_URL}/${rankType}`);
    const $ = cheerio.load(data);
    const title = $('.toplist__tit').text();
    const songList = $('.songlist__list li').each(async (index, item) => {
        if (index < 1) {
            const $item = cheerio.load(item);
            const songName = $item('.songlist__cover').attr('title');
            const description = $item('.songlist__song_txt').text();
            const songTime = $item('.songlist__time').text();
            const detailPath = $item('.songlist__cover').attr('href');
            const detailUr = `${DOMAIN}${detailPath}`
            const commentList = await getCommentList(detailUr);
        }
     
        console.log(songName, description, songTime, detailUr);
    });
    return data;
}

const getCommentList = async (detailUr) => {
    try {
        const { data } = await axios.get(detailUr);
        console.log(data)
        const $ = cheerio.load(data);
        const commentList = $('.comment__list');
        const list = [];
        console.log(commentList.children())
        commentList.each((index, item) => {
            // 动态渲染的评论无法爬取 需要使用其他方式
            // const $item = cheerio.load(item);
            //     console.log($item)
            //     const comment = $item('.comment__text ').text();
            //     const userName = $item('.comment__title').text();
            //     list.push({
            //         userName,
            //         comment
            //     })
        })
        return list;
    } catch (error) {
        console.log(error)
    }

}


const main = async () => {
    // 解析参数
    const rankType = process.argv.slice(2)[0];
    // 构造一个url 并请求
    const data = await getRankListData(rankType);
    // 解析返回的内容，获取我们需要的数据
}
main()